package com.glory.wxelectricitypda.activity;

import android.support.v4.app.Fragment;
import android.widget.LinearLayout;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BoxLabelReviewBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.fragment.PackagedFragment;
import com.glory.wxelectricitypda.fragment.UnpackagedFragment;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabEntity;
import com.pda.platform.ui.ui_pdaplatform.tablayout.CommonTabLayout;
import com.pda.platform.ui.ui_pdaplatform.tablayout.listener.CustomTabEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;

public class BoxLabelReviewActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.containerNum)
    FreeUI_ClearEditText containerNum;
    @BindView(R.id.boxNum)
    FreeUI_ClearEditText boxNum;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;

    private PackagedFragment packagedFragment;
    private UnpackagedFragment unpackagedFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_box_label_review;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("箱标签审核");

        unpackagedFragment = new UnpackagedFragment();
        mFragments.add(unpackagedFragment);
        packagedFragment = new PackagedFragment();
        mFragments.add(packagedFragment);
        mTabEntities.add(new FreeUI_TabEntity("已审核盒号",
                null, null));
        mTabEntities.add(new FreeUI_TabEntity("已审核箱号",
                null, null));
        tabLayout.setTabData(mTabEntities, this, R.id.content, mFragments);
    }

    @Override
    protected void initData() {

        initEdit(boxNum, s -> {

            if (etUser.getText().toString().isEmpty()) {
                showToast("请输入工号", false);
                return;
            }
//            if (lotID.getText().toString().isEmpty()) {
//                showToast("请扫描批次号", false);
//                return;
//            }
            if (containerNum.getText().toString().isEmpty()) {
                showToast("请扫描箱号", false);
                return;
            }

            queryBoxLabelReview();
        });

    }


    private void queryBoxLabelReview() {


        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "APPROVE");
//            map.put("LOTID", lotID.getText().toString());
            map.put("CHECKPACKLOTID", boxNum.getText().toString());
            map.put("PARENTPACKLOTID", containerNum.getText().toString());
            map.put("CHECKTYPE", "BOX");
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELCHECK");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BoxLabelReviewBean>>(
                new TypeToken<ResponseBean<BoxLabelReviewBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BoxLabelReviewBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    boolean types = false;
                    for (BoxLabelReviewBean.XPACKAGEDETAILDTO bean: unpackagedFragment.mUnpackagedAdapter.getData()) {
                        if (bean.getPACKLOTID().equals(response.body().getBody().getXPACKAGEDETAIL().getPACKLOTID())){
                            types = true;
                        }
                    }
                    if (types){
                        showToast("该箱号以添加",false);
                        boxNum.setText("");
                        return;
                    }
                    unpackagedFragment.addData(response.body().getBody().getXPACKAGEDETAIL());
                    if (response.body().getBody().getCHECKFINISH()){
                        boolean type = false;
                        for (String s: packagedFragment.mPackagedAdapter.getData()) {
                            if (s.equals(containerNum.getText().toString())){
                                type = true;
                            }
                        }
                        if (type){
                            showToast("该箱号以审核",false);
                            boxNum.setText("");
//                            lotID.setText("");
                            containerNum.setText("");
                            return;
                        }
                        packagedFragment.addData(containerNum.getText().toString());
//                        unpackagedFragment.mUnpackagedAdapter.setNewData(null);
                        boxNum.setText("");
//                        lotID.setText("");
                        containerNum.setText("");
                    }else {
                        boxNum.setText("");
                    }

                }
            }

            @Override
            public void onError(Response<ResponseBean<BoxLabelReviewBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELCHECK");
            }
        });


    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}